package com.lucun.es_transaction.service.impl;

import com.lucun.es_transaction.entity.Address;
import com.lucun.es_transaction.entity.UserLog;
import com.lucun.es_transaction.mapper.AddressMapper;
import com.lucun.es_transaction.mapper.UserLogMapper;
import com.lucun.es_transaction.service.AddressService;
import com.lucun.es_transaction.util.BaseException;
import com.lucun.es_transaction.util.JsonResult;
import com.lucun.es_transaction.util.JwtUtil;
import com.lucun.es_transaction.util.RedisUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.javassist.tools.web.BadHttpRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Service
public class AddressServiceImpl implements AddressService {
    @Autowired
    UserLogMapper userLogMapper;
    @Autowired
    AddressMapper addressMapper;
    /*添加*/
    @Override
    public JsonResult<?> insert(Address address, HttpServletRequest request) {
        if (ObjectUtils.isEmpty(address)){
            throw new BaseException("数据为空");
        }
        int idByToken = JwtUtil.getIdByToken(request);
        if (ObjectUtils.isEmpty(idByToken)){
            throw new BaseException("解析token失败");
        }
        String numberToken = JwtUtil.getNumberByToken(request);
        if (ObjectUtils.isEmpty(numberToken)){
            throw new BaseException("解析number失败");
        }
        String address1 = address.getAddress();
        if (StringUtils.isEmpty(address1)){
            throw new BaseException("没有获取地址信息");
        }
        address.setUserNumber(numberToken);
        address.setCreateTime(new Date());
        int i = addressMapper.insert(address);
        if (i>0){
            UserLog userLog = new UserLog();
            userLog.setLogName("添加地址成功");
            userLog.setLogType((byte)1);
            userLog.setLogStatus((byte)1);
            userLog.setUserId(idByToken);
            userLog.setCreateTime(new Date());
            userLogMapper.insertSelective(userLog);
        }else {
            UserLog userLog = new UserLog();
            userLog.setLogName("添加地址失败");
            userLog.setLogType((byte)1);
            userLog.setLogStatus((byte)0);
            userLog.setUserId(idByToken);
            userLog.setCreateTime(new Date());
            userLogMapper.insertSelective(userLog);
        }
        if (i>0){
            return new JsonResult<>("200","添加成功",address);
        }else {
            return new JsonResult<>("500","添加失败",address);
        }
    }
/* 删除 */
    @Override
    public JsonResult<?> delete(Integer id,HttpServletRequest request) {
        if (ObjectUtils.isEmpty(id)){
            throw new BaseException("没有获取此id");
        }
        int idByToken = JwtUtil.getIdByToken(request);
        if (ObjectUtils.isEmpty(idByToken)){
            throw new BaseException("解析token失败");
        }
        int i = addressMapper.deleteByPrimaryKey(id);
        if (i>0){
            UserLog userLog = new UserLog();
            userLog.setLogName(idByToken+"删除地址"+id+"成功");
            userLog.setLogType((byte)1);
            userLog.setLogStatus((byte)2);
            userLog.setUserId(idByToken);
            userLog.setCreateTime(new Date());
            userLogMapper.insertSelective(userLog);
        }else {
            UserLog userLog = new UserLog();
            userLog.setLogName(idByToken+"删除地址"+id+"失败");
            userLog.setLogType((byte)2);
            userLog.setLogStatus((byte)0);
            userLog.setUserId(idByToken);
            userLog.setCreateTime(new Date());
            userLogMapper.insertSelective(userLog);
        }
        if (i>0){
            return new JsonResult<>("200","删除成功",id);
        }else {
            return new JsonResult<>("500","删除失败",id);
        }
    }
/* 修改 */
    @Override
    public JsonResult<?> update(Address address, HttpServletRequest request) {
        int id = address.getId();
        if (ObjectUtils.isEmpty(id)){
            throw new BaseException("没有获取id");
        }
        int idtoken = JwtUtil.getIdByToken(request);
        if (ObjectUtils.isEmpty(idtoken)){
            throw new BaseException("解析token失败");
        }
        address.setUpdateTime(new Date());
        int i = addressMapper.update(address);
        if (i>0){
            UserLog userLog = new UserLog();
            userLog.setLogName(idtoken+"修改地址"+id+"成功");
            userLog.setLogType((byte)1);
            userLog.setLogStatus((byte)2);
            userLog.setUserId(idtoken);
            userLog.setCreateTime(new Date());
            userLogMapper.insertSelective(userLog);
        }else {
            UserLog userLog = new UserLog();
            userLog.setLogName(idtoken+"修改地址"+id+"失败");
            userLog.setLogType((byte)2);
            userLog.setLogStatus((byte)0);
            userLog.setUserId(idtoken);
            userLog.setCreateTime(new Date());
            userLogMapper.insertSelective(userLog);
        }
        if (i>0){
            return new JsonResult<>("200","修改成功",id);
        }else {
            return new JsonResult<>("500","修改失败",id);
        }
    }
/*查询*/
    @Override
    public List<Address> find(HttpServletRequest request) {
        int idtoken = JwtUtil.getIdByToken(request);
        if (ObjectUtils.isEmpty(idtoken)){
            throw new BaseException("解析token失败");
        }
        int userId = JwtUtil.getIdByToken(request);
        return addressMapper.find(userId);
    }
}
